﻿using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Navigation;
using WPNEXT.Common;
using WPNEXT.Model;

namespace WPNEXT.ViewModel
{
    public class SimpleViewModel : BaseViewModel
    {
        #region Fields
        private ObservableCollection<ImageModel> _items = new ObservableCollection<ImageModel>();
        private bool _loading = false;
        private RelayCommand _getDataCommand;
        private RelayCommand _resetDataCommand;
        #endregion

        #region Constructor
        public SimpleViewModel()
        {
            InitItem();
        }

        private void InitItem()
        {
            List<ImageModel> list = new List<ImageModel>();
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15866793897_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19765708787_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao15848113020_146x146.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15848325228_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.03, Url = "http://img.geilicdn.com/taobao19779004225_146x150.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.57, Url = "http://img.geilicdn.com/taobao15865634518_146x229.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao15865434962_146x144.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15830113145_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.15, Url = "http://img.geilicdn.com/taobao17077543430_146x167.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.46, Url = "http://img.geilicdn.com/taobao19726272617_146x213.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.12, Url = "http://img.geilicdn.com/taobao15865557654_146x163.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.38, Url = "http://img.geilicdn.com/taobao19776424457_146x201.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao19712332041_146x144.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao19722332410_146x146.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15851750545_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.35, Url = "http://img.geilicdn.com/taobao15857918167_146x197.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao19704356816_146x223.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19725572516_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao19725916030_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.60, Url = "http://img.geilicdn.com/taobao15845742605_146x233.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.41, Url = "http://img.geilicdn.com/taobao15842817755_146x205.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15835866335_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao15693786979_146x223.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao17072171509_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.67, Url = "http://img.geilicdn.com/taobao17072151693_146x97.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.51, Url = "http://img.geilicdn.com/taobao15882494639_146x220.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.49, Url = "http://img.geilicdn.com/taobao19813600740_146x217.jpg" });

            list.ToObservableCollection<ImageModel>(this.Items);
        }
        #endregion

        #region Properties
        public ObservableCollection<ImageModel> Items
        {
            get { return _items; }
            set
            {
                if (value != _items)
                {
                    _items = value;
                    this.RaisePropertyChanged(() => this.Items);
                }
            }
        }

        public bool Loading
        {
            get { return _loading; }
            set
            {
                if (value != _loading)
                {
                    _loading = value;
                    this.RaisePropertyChanged(() => this.Loading);
                }
            }
        }
        #endregion

        #region Command
        public RelayCommand GetDataCommand
        {
            get
            {
                if (this._getDataCommand == null)
                {
                    _getDataCommand = new RelayCommand(GetData);
                }
                return _getDataCommand;
            }
            private set { _getDataCommand = value; }
        }

        public RelayCommand ResetDataCommand
        {
            get
            {
                if (this._resetDataCommand == null)
                {
                    _resetDataCommand = new RelayCommand(ResetDataExecute);
                }
                return _resetDataCommand;
            }
            private set { _resetDataCommand = value; }
        }
        
        #endregion

        #region Override
        public override void OnNavigatedTo(PhoneApplicationPage currentPage, NavigationEventArgs e)
        {
            if (e.NavigationMode == NavigationMode.Back)
            {
                //back get data
                if (currentPage.State.ContainsKey("Items") && 0 >= Items.Count)
                {
                    this.Items = currentPage.State["Items"] as ObservableCollection<ImageModel>;
                }
                currentPage.State.Remove("Items");
            }
            else
            {
                GetData();
            }
        }

        public override void OnNavigatedFrom(PhoneApplicationPage currentPage, NavigationEventArgs e)
        {
            if (e.NavigationMode != NavigationMode.Back)
            {
                //save data
                currentPage.State["Items"] = this._items;
                return;
            }
            //clear data
            this._items.Clear();
            this._loading = false;
            GetDataCommand = null;
            ResetDataCommand = null;
        }
        #endregion

        #region Method
        private void GetData()
        {
            List<ImageModel> list = new List<ImageModel>();
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15866793897_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19765708787_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao15848113020_146x146.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15848325228_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.03, Url = "http://img.geilicdn.com/taobao19779004225_146x150.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.57, Url = "http://img.geilicdn.com/taobao15865634518_146x229.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao15865434962_146x144.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15830113145_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.15, Url = "http://img.geilicdn.com/taobao17077543430_146x167.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.46, Url = "http://img.geilicdn.com/taobao19726272617_146x213.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.12, Url = "http://img.geilicdn.com/taobao15865557654_146x163.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.38, Url = "http://img.geilicdn.com/taobao19776424457_146x201.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.99, Url = "http://img.geilicdn.com/taobao19712332041_146x144.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.00, Url = "http://img.geilicdn.com/taobao19722332410_146x146.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15851750545_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.35, Url = "http://img.geilicdn.com/taobao15857918167_146x197.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao19704356816_146x223.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao19725572516_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao19725916030_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.60, Url = "http://img.geilicdn.com/taobao15845742605_146x233.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.41, Url = "http://img.geilicdn.com/taobao15842817755_146x205.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.50, Url = "http://img.geilicdn.com/taobao15835866335_146x219.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.53, Url = "http://img.geilicdn.com/taobao15693786979_146x223.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.33, Url = "http://img.geilicdn.com/taobao17072171509_146x194.jpg" });
            list.Add(new ImageModel { ImgRatio = 0.67, Url = "http://img.geilicdn.com/taobao17072151693_146x97.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.51, Url = "http://img.geilicdn.com/taobao15882494639_146x220.jpg" });
            list.Add(new ImageModel { ImgRatio = 1.49, Url = "http://img.geilicdn.com/taobao19813600740_146x217.jpg" });

            list.ToObservableCollection<ImageModel>(this.Items);
        }

        private void ResetDataExecute()
        {
            this.Items.Clear();
        }
       
        #endregion
    }
}
